package lc.com.lcpicturebackend.manager.app;

import cn.hutool.json.JSONUtil;
import lc.com.lcpicturebackend.model.entity.ConversationMsg;
import org.springframework.ai.chat.messages.*;
import org.stringtemplate.v4.ST;

import java.util.List;
import java.util.Map;

/**
 * 功能：Message转换
 * 作者：lc
 * 日期：2025/6/1 14:13
 */
public class MessageConverter {

    /**
     * ConversationMsg转Message
     *
     * @param conversationMsg 会话消息类
     * @return Message
     */
    public static Message objToMessage(ConversationMsg conversationMsg) {
        String role = conversationMsg.getRole();
        String content = conversationMsg.getContent();
        Map<String,Object> metadata = JSONUtil.toBean(conversationMsg.getMetadata(), Map.class);
        return switch (role) {
            case "user" -> new UserMessage(content);
            case "assistant" -> new AssistantMessage(content,metadata);
            case "system" -> new SystemMessage(content);
            default -> new ToolResponseMessage(List.of(), metadata);
        };

    }

    /**
     * 将 Message 转换为 ChatMessage
     */
    public static ConversationMsg messageToObj(Message message, String conversationId) {
        return ConversationMsg.builder()
                .conversationId(conversationId)
                .role(message.getMessageType().getValue())
                .metadata(JSONUtil.toJsonStr(message.getMetadata()))
                .content(message.getText())
                .build();
    }

}